/*
 * vws_http_response.cpp
 *
 *  Created on: Sep 20, 2013
 *      Author: vscen
 */

#include "vws_http_response.h"
#include "md5.h"
#include "base64.h"

namespace vws {
bool vws_http_response::getRetryAfterField(int nSecond, char* pszBuffer,
		size_t nSize) {
	if (nSize < 32) {
		return false;
	} else {
		memset(pszBuffer, 0, nSize);
		snprintf(pszBuffer, 32, "Retry-After: %d\r\n", nSecond);
		return true;
	}
}
bool vws_http_response::getServerField(char* pszBuffer, size_t nSize) {
	if (nSize < 20) {
		return false;
	} else {
		memset(pszBuffer, 0, nSize);
		strcpy(pszBuffer, "Server: ");
		strcat(pszBuffer, HTTPSERVERVERSION);
		strcat(pszBuffer, "\r\n");
		return true;
	}
}
bool vws_http_response::getAgeField(char * seconds, char* pszBuffer,
		size_t nSize) {
	if (nSize < 0x32) {
		return false;
	} else {
		char* psrc = seconds;
		while (*psrc) {
			if (isdigit(*psrc) != 0)
				psrc++;
			else
				return false;
		}
		memset(pszBuffer, 0, nSize);
		strcpy(pszBuffer, "Age: ");
		strncat(pszBuffer, seconds, psrc - seconds);
		strncat(pszBuffer, "\r\n", 2);
		return true;
	}
}
bool vws_http_response::getUserAgentField(char* pszBuffer, size_t nSize) {
	if (nSize < 0x32) {
		return false;
	} else {
		memset(pszBuffer, 0, nSize);
		strcpy(pszBuffer, "User-Agent: ");
		strcat(pszBuffer, HTTPSERVERVERSION);
		strncat(pszBuffer, "\r\n", 2);
		return true;
	}
}
bool vws_http_response::getDateField(char* pszBuffer, size_t nSize) {
	if (nSize < 0x32) {
		return false;
	} else {
		char pszBuf[64] = { 0 };
		memset(pszBuffer, 0, nSize);
		strcpy(pszBuffer, "Date: ");
		if (vws_http_time::getDateStyle(0, pszBuf, 64) == false) {
			return false;
		}
		//RFC1123_DATE
		strcat(pszBuffer, pszBuf);
		strncat(pszBuffer, "\r\n", 2);
		return true;
	}
}
bool vws_http_response::getEntityTag(char * data, char* pszBuffer, size_t nSize) {
	if (nSize < 64) {
		return false;
	} else {
		memset(pszBuffer, 0, nSize);
		snprintf(pszBuffer, nSize, "ETag: %s\r\n", data);
		return true;
	}
}
bool vws_http_response::getFormMailField(char* e_mail, char* pszBuffer,
		size_t nSize) {
	if (e_mail == NULL || nSize < 48) {
		return false;
	} else {
		memset(pszBuffer, 0, nSize);
		strcpy(pszBuffer, "From: ");
		snprintf(pszBuffer + 6, 32, ":%s", e_mail);
		return true;
	}
}
bool vws_http_response::getContentEncodingField(int nIndex, char* pszBuffer,
		size_t nSize) {
	if (nSize < 64) {
		return false;
	} else {
		memset(pszBuffer, 0, nSize);
		strcpy(pszBuffer, "Content-Encoding: ");
		if (nIndex == 0) {
			strcat(pszBuffer, "gzip\r\n");
		} else if (nIndex == 1) {
			strcat(pszBuffer, "compress\r\n");
		} else if (nIndex == 2) {
			strcat(pszBuffer, "deflate\r\n");
		} else if (nIndex == 3) {
			strcat(pszBuffer, "identity\r\n");
		} else {
			return false;
		}
		return true;
	}
}
bool vws_http_response::getContentLengthField(size_t len, char* pszBuffer,
		size_t nSize) {
	if (nSize < 64) {
		return false;
	} else {
		memset(pszBuffer, 0, nSize);
		strcpy(pszBuffer, "Content-Length: ");
		char* temp_ptr = (char*) (pszBuffer + strlen(pszBuffer));
		sprintf(temp_ptr, "%ld", len);
		strncat(temp_ptr, "\r\n", 2);
		return true;
	}
}
bool vws_http_response::getContentMd5(unsigned char * src, size_t length,
		char* pszBuffer, size_t nSize) {
	if (nSize < 64) {
		return false;
	} else {
		md5* pMd5 = new md5;
		if (pMd5 == NULL) {
			return false;
		}
		base64* pBase = new base64;
		if (pBase == NULL) {
			delete pMd5;
			return false;
		}
		char pszMd5[48] = { 0 };
		memset(pszBuffer, 0, nSize);
		strcpy(pszBuffer, "Content-MD5: ");
		pMd5->calculate((unsigned char*) pszMd5, src, length);
		unsigned char* pszBasic64 = (unsigned char*) (pszBuffer + strlen(
				pszBuffer));
		pBase->encoder(pszBasic64, (unsigned char*) pszMd5, strlen(pszMd5));
		strncat((char*) pszBasic64, "\r\n", 2);
		if (pMd5 != NULL) {
			delete pMd5;
		}
		if (pBase != NULL) {
			delete pBase;
		}
		return true;
	}
}
bool vws_http_response::getExpiresField(char* pszBuffer, size_t nSize) {
	if (nSize < 0x32) {
		return false;
	} else {
		char pszBuf[64] = { 0 };
		memset(pszBuffer, 0, nSize);
		strcpy(pszBuffer, "Expires: ");
		if (vws_http_time::getDateStyle(0, pszBuf, 64) == false) {
			return false;
		}
		strcat(pszBuffer, pszBuf);
		strncat(pszBuffer, "\r\n", 2);
		return true;
	}
}
bool vws_http_response::getConnectField(int nIndex, char* pszBuffer,
		size_t nSize) {
	if (nSize < 64) {
		return false;
	} else {
		memset(pszBuffer, 0, nSize);
		if (nIndex == 0) {
			strcat(pszBuffer, "close\r\n");
		} else if (nIndex == 1) {
			strcat(pszBuffer, "keep-live\r\n");
		} else {
			return false;
		}
		return true;
	}
}
bool vws_http_response::getAcceptRangesField(int nIndex, char* pszBuffer,
		size_t nSize) {
	if (nSize < 64) {
		return false;
	} else {
		memset(pszBuffer, 0, nSize);
		strcpy(pszBuffer, "Accept-Ranges: ");
		if (nIndex == 0) {
			strcat(pszBuffer, "bytes\r\n");
		} else if (nIndex == 1) {
			strcat(pszBuffer, "none\r\n");
		} else {
			return false;
		}
		return true;
	}
}
bool vws_http_response::getAllowMethod(char* pszBuffer, size_t nSize) {
	if (nSize < 64) {
		return false;
	} else {
		memset(pszBuffer, 0, nSize);
		strcpy(pszBuffer, "Allow: GET,HEAD\r\n");
		return true;
	}
}
}
